//cache缓存
import Cache from '@/utils/cache.js';
//语言
import Lang from '@/utils/lang.js';
//登录
import Login from '@/lib/login.js';
//登录
import HitLogin from '@/lib/hitlogin.js';
//基础配置
import APP from '@/config/app.js'

//公共输出
const output = function(data) {
	return data;
}
//随机数生成
const randNumber = function(min, max) {
	return Math.floor(Math.random() * (max - min + 1)) + min;
}
//判断网络
const ifnetwork = function() {
	return new Promise((resolve, reject) => {
		uni.getNetworkType({
			success: function(res) {
				//没有网络
				if (res.networkType == 'none') {
					resolve(false);
				} else {
					resolve(true);
				}
			}
		})
	})
}
//打榜赛全局授权
const hitglobalauthorization = function() {
	if (!Cache.has('user_openid')) {
		uni.showModal({
			title: Lang.lang.authorisation,
			content: Lang.lang.authorisationdes,
			confirmText: Lang.lang.confirmtext,
			showCancel: false,
			success: function(res) {
				if (res.confirm) {
					HitLogin.weixinglogin();
				}
			}
		})
	}
}
//全局授权
const globalauthorization = function() {
	if (!Cache.has('user_openid')) {
		uni.showModal({
			title: Lang.lang.authorisation,
			content: Lang.lang.authorisationdes,
			confirmText: Lang.lang.confirmtext,
			showCancel: false,
			success: function(res) {
				if (res.confirm) {
					Login.weixinglogin();
				}
			}
		})
	}
}
//公共弹出
const publicpopup = function(title = '', content = '', url = '') {
	uni.showModal({
		title: title,
		content: content,
		showCancel: false,
		success: function(res) {
			if (url) {
				uni.redirectTo({
					url: url
				})
			}
		}
	});
}
//公共成功弹出
const publicshowtoastsuccess = function(title = '', url = '') {
	uni.showToast({
		title: title,
		icon: 'success',
	});
	setTimeout(function() {
		if (url) {
			uni.redirectTo({
				url: url
			})
		}
		uni.hideLoading();
	}, 2000);

}
//公共失败弹出
const publicshowtoasterror = function(title = '', url = '') {
	uni.showToast({
		title: title,
		icon: 'error',
	});
	setTimeout(function() {
		uni.hideLoading();
		if (url) {
			uni.redirectTo({
				url: url
			})
		}
	}, 2000);
}
//当前年月日时间戳
const nowymd = function() {
	var date = new Date();
	var year = date.getFullYear(); //年
	var month = date.getMonth() + 1; //月
	var day = date.getDate(); //日
	var timeDate = year + "-" + month + "-" + day + " 0:0:0";
	return timeDate;
}
//用户画像缓存个数
const portrayalcount = function() {
	//用户画像缓存个数
	if (!Cache.has('portrayalcount')) {
		Cache.set('portrayalcount', 1);
	}
	//用户画像缓存个数加
	var portrayalcount = Cache.get('portrayalcount');
	++portrayalcount;
	Cache.set('portrayalcount', portrayalcount);
	return portrayalcount;
}
//用户画像
const portrayal = function(url) {
	var that = this;
	//判断并推送用户画像
	that.portrayalsend();
	//用户画像缓存个数
	var portrayalcount = that.portrayalcount();
	//当前年月日时间戳
	var timestemp = Cache.strTotime(that.nowymd());
	//保存缓存
	Cache.set('portrayaltime', timestemp);
	//当前时间戳
	var nowtime = Cache.time();
	//保存用户画像缓存
	Cache.set('portrayal-' + timestemp + portrayalcount, nowtime + "-" + url);
}
//获取当前页面url
const getUrl = function() {
	var pages = getCurrentPages() //获取加载的页面
	var url = '';
	if (pages.length > 0) {
		var currentPage = pages[pages.length - 1] //获取当前页面的对象
		var url = currentPage.route //当前页面url
		url = url;
	}
	return url;
}


//判断并推送用户画像
const portrayalsend = function() {
	var that = this;
	var portrayaltime = Cache.get('portrayaltime');
	//当前年月日时间戳
	var timestemp = Cache.strTotime(that.nowymd());
	//判断是否推送
	if (timestemp > portrayaltime) {
		//运行平台
		var platform = uni.getSystemInfoSync().platform;
		//运行设备
		var deviceModel = uni.getSystemInfoSync().deviceModel;
		//运行设备编码
		var deviceId = uni.getSystemInfoSync().deviceId;
		//发送
		Cache.set('portrayalcount', 1)
	}
}
//洗牌函数 source 是一个数组
const shuffle = function(source) {
	// 先复制一份避免直接修改source
	const arr = source.slice()
	for (let i = 0; i < arr.length; i++) {
		const j = this.getRandomInt(i)
		// 交换arr[i]和j的值 交换后会修改arr数组
		swap(arr, i, j)
	}
	return arr
}
//取0-max之间的值也会包含max
const getRandomInt = function(max) {
	// Math.random() 0-1之间值 但不包含1
	return Math.floor(Math.random() * (max + 1))
}
//交换arr[i]和arr[j]的值
const swap = function(arr, i, j) {
	const t = arr[i]
	arr[i] = arr[j]
	arr[j] = t
}
//重新加载
const reload = function() {
	// 页面重载
	const pages = getCurrentPages()
	// 声明一个pages使用getCurrentPages方法
	const curPage = pages[pages.length - 1]
	// 声明一个当前页面
	curPage.onLoad(curPage.options) // 传入参数
	curPage.onShow()
	curPage.onReady()
	// 执行刷新
}
//无解有解
const ifsolution = function(numbers) {
	var ivsNumber = 0;
	//遍历数组
	for (var i = 0; i < numbers.length; i++) {
		for (var j = i + 1; j < numbers.length; j++) {
			//依次两两对比,判断前一个是否比后一个大
			if (numbers[i] > numbers[j]) {
				//是,则ivsNumber加1
				ivsNumber++;
			}
		}
	}
	//判断ivsNumber变量是否为偶数
	if (ivsNumber % 2 == 0) {
		//是函数返回true
		return true;
	} else {
		//不是函数返回false
		return false;
	}
}
//“yyyy-MM-dd hh:mm:ss”转换成  yyyy-MM-dd
const timeProcessing_getdate = function(timeDate) {
	 var that=this;
	 var data=that.getdate(that.timeProcessing(timeDate));
	return data;
}


//“yyyy-MM-dd hh:mm:ss”转换成时间戳
const timeProcessing = function(timeDate) {
	let Time = new Date(timeDate);
	let timestemp = Time.getTime();
	return timestemp;
}

//使用的时间戳转换成日期（年月日）
const getdate = function(value) {
	if (!value) return '';
	value = parseInt(value)
	var now = new Date(value);
	var year = now.getFullYear();
	var month = now.getMonth() + 1;
	if (month < 10) {
		month = '0' + month
	}
	var date = now.getDate();
	if (date < 10) {
		date = '0' + date
	}
	return year + "-" + month + "-" + date
}


export default {
	output,
	randNumber,
	portrayal,
	getUrl,
	nowymd,
	portrayalsend,
	portrayalcount,
	ifnetwork,
	publicpopup,
	globalauthorization,
	publicshowtoastsuccess,
	publicshowtoasterror,
	shuffle,
	hitglobalauthorization,
	getRandomInt,
	ifsolution,
	reload,
	timeProcessing,
	getdate,
	timeProcessing_getdate,
}